package com.lmh.graphdisease.service.impl;

import com.lmh.graphdisease.entity.*;
import com.lmh.graphdisease.repository.*;
import com.lmh.graphdisease.service.DepartmentService;
import com.lmh.graphdisease.service.DiseaseService;
import com.lmh.graphdisease.service.DrugService;
import com.lmh.graphdisease.service.OtherService;
import com.lmh.graphdisease.vo.OtherEntityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author lmh
 * @desc
 * @date 2023/4/13
 */
@Service
public class OtherServiceImpl implements OtherService {

    @Autowired
    private DiseaseRepository diseaseRepository;

    @Autowired
    private FoodRepository foodRepository;

    @Autowired
    private CheckRepository checkRepository;

    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private DrugRepository drugRepository;

    @Autowired
    private SymptomRepository symptomRepository;

    @Override
    public List<OtherEntityVO> getPageOther(String identity,int page, int size) {
        List<OtherEntityVO> result = new ArrayList<>();
        if(Objects.equals(identity, "Food")){
            List<FoodEntity> foodEntities = foodRepository.getNumFood(page,size);
            for (FoodEntity f:foodEntities){
                result.add(new OtherEntityVO(f.getId(),f.getName()));
            }
        }else if(Objects.equals(identity, "Check")){
            List<CheckEntity> checkEntities = checkRepository.getNumCheck(page,size);
            for (CheckEntity c:checkEntities){
                result.add(new OtherEntityVO(c.getId(),c.getName()));
            }
        }else if(Objects.equals(identity, "Department")){
            List<DepartmentEntity> departmentEntities = departmentRepository.getNumDepartment(page,size);
            for (DepartmentEntity d:departmentEntities){
                result.add(new OtherEntityVO(d.getNodeId(),d.getName()));
            }
        }else if(Objects.equals(identity, "Drug")){
            List<DrugEntity> drugEntities = drugRepository.getNumDrug(page, size);
            for (DrugEntity d:drugEntities){
                result.add(new OtherEntityVO(d.getId(),d.getName()));
            }
        }else if(Objects.equals(identity, "Symptom")){
            List<SymptomEntity> symptomEntities = symptomRepository.getNumSymptom(page,size);
            for (SymptomEntity s:symptomEntities){
                result.add(new OtherEntityVO(s.getId(),s.getName()));
            }
        }
        return result;
    }

    @Override
    public Integer getOtherNum(String identity) {
        if(Objects.equals(identity, "Food")){
            return foodRepository.getFoodNum();
        }else if(Objects.equals(identity, "Check")){
            return checkRepository.getCheckNum();
        }else if(Objects.equals(identity, "Department")){
            return departmentRepository.getDepartmentNum();
        }else if(Objects.equals(identity, "Drug")){
            return drugRepository.getDrugNum();
        }else if(Objects.equals(identity, "Symptom")){
            return symptomRepository.getSymptomNum();
        }else {
            return 0;
        }

    }

    @Override
    public boolean addOtherLabel(String identity, String name) {
        if(Objects.equals(identity, "Food")){
            foodRepository.addFood(name);
            return true;
        }else if(Objects.equals(identity, "Check")){
            checkRepository.addCheck(name);
            return true;
        }else if(Objects.equals(identity, "Department")){
            departmentRepository.addDepartment(name);
            return true;
        }else if(Objects.equals(identity, "Drug")){
            drugRepository.addDrug(name);
            return true;
        }else if(Objects.equals(identity, "Symptom")){
            symptomRepository.addSymptom(name);
            return true;
        }else {
            return false;
        }
    }

    /**
     *@author lmh
     *@date 2023/4/17
     *@desc 删除没有构建关系的节点
     */
    @Override
    public boolean deleteOtherNoRelationship(String identity,String name) {
        if(Objects.equals(identity, "Food")){
            FoodEntity foodEntity = foodRepository.getFoodByName(name);
            if(foodEntity == null){
                return false;
            }
            foodRepository.deleteByName(foodEntity.getName());
            return true;
        }else if(Objects.equals(identity, "Check")){
            CheckEntity checkEntity = checkRepository.getCheckByName(name);
            if(checkEntity == null){
                return false;
            }
            checkRepository.deleteByName(checkEntity.getName());
            return true;
        }else if(Objects.equals(identity, "Department")){
            DepartmentEntity departmentEntity = departmentRepository.getDepartmentByName(name);
            if(departmentEntity == null){
                return false;
            }
            departmentRepository.deleteByName(departmentEntity.getName());
            return true;
        }else if(Objects.equals(identity, "Drug")){
            DrugEntity drugEntity = drugRepository.getDrugByName(name);
            if(drugEntity == null){
                return false;
            }
            drugRepository.deleteByName(drugEntity.getName());
            return true;
        }else if(Objects.equals(identity, "Symptom")){
            SymptomEntity symptomEntity = symptomRepository.getSymptomByName(name);
            if(symptomEntity == null){
                return false;
            }
            symptomRepository.deleteByName(symptomEntity.getName());
            return true;
        }else {
            return false;
        }
    }

    /**
     *@author lmh
     *@date 2023/4/17
     *@desc 标识，名称，查询相关的数目
     */
    @Override
    public Map<String, Object> getOneOther(int identity, String name,int num) {
        Map<String,Object> result = new HashMap<>();
        if(identity == 1){//科室
            DepartmentEntity departmentEntity = departmentRepository.getDepartmentByName(name);
            if(departmentEntity == null) return null;
            List<DiseaseEntity> diseaseEntities = diseaseRepository.getDepartmentDisease(name,num);
            Set<String> diseaseSet = new HashSet<>();
            Set<String> departmentSet = new HashSet<>();
            departmentSet.add(departmentEntity.getName());
            List<String> relationShips = new ArrayList<>();
            for (DiseaseEntity diseaseEntity:diseaseEntities){
                diseaseSet.add(diseaseEntity.getId() + "::" + diseaseEntity.getName());
                relationShips.add(diseaseEntity.getId() + "-*-" + "所属科室" + "-*-" + departmentEntity.getName());
            }
            result.put("disease",diseaseSet);
            result.put("department",departmentSet);
            result.put("relationships",relationShips);
        }else if(identity == 2){//检查
            CheckEntity checkEntity = checkRepository.getCheckByName(name);
            if(checkEntity == null) return null;
            List<DiseaseEntity> diseaseEntities = diseaseRepository.getCheckDisease(name,num);
            Set<String> diseaseSet = new HashSet<>();
            Set<String> checkSet = new HashSet<>();
            checkSet.add(checkEntity.getName());
            List<String> relationShips = new ArrayList<>();
            for (DiseaseEntity diseaseEntity:diseaseEntities){
                diseaseSet.add(diseaseEntity.getId() + "::" + diseaseEntity.getName());
                relationShips.add(diseaseEntity.getId() + "-*-" + "需要检查" + "-*-" + checkEntity.getName());
            }
            result.put("disease",diseaseSet);
            result.put("check",checkSet);
            result.put("relationships",relationShips);
        }else if(identity == 3){//药品
            DrugEntity drugEntity = drugRepository.getDrugByName(name);
            if(drugEntity == null) return null;
            List<DiseaseEntity> diseaseEntities = diseaseRepository.getDrugCommonDisease(name,num);
            diseaseEntities.addAll(diseaseRepository.getDrugRecommandDisease(name,num));
            Set<String> diseaseSet = new HashSet<>();
            Set<String> drugSet = new HashSet<>();
            drugSet.add(drugEntity.getName());
            List<String> relationShips = new ArrayList<>();
            for (DiseaseEntity diseaseEntity:diseaseEntities){
                diseaseSet.add(diseaseEntity.getId() + "::" + diseaseEntity.getName());
                relationShips.add(diseaseEntity.getId() + "-*-" + "所需药物" + "-*-" + drugEntity.getName());
            }
            result.put("disease",diseaseSet);
            result.put("drug",drugSet);
            result.put("relationships",relationShips);
        }else if(identity == 4){//食物
            FoodEntity foodEntity = foodRepository.getFoodByName(name);
            if(foodEntity == null) return null;
            List<DiseaseEntity> diseaseEntities1 = diseaseRepository.getDoFoodDisease(name,num);
            List<DiseaseEntity> diseaseEntities2 = diseaseRepository.getNotFoodDisease(name,num);
            Set<String> diseaseSet = new HashSet<>();
            Set<String> foodSet = new HashSet<>();
            foodSet.add(foodEntity.getName());
            List<String> relationShips = new ArrayList<>();
            for (DiseaseEntity diseaseEntity:diseaseEntities1){
                diseaseSet.add(diseaseEntity.getId() + "::" + diseaseEntity.getName());
                relationShips.add(diseaseEntity.getId() + "-*-" + "推荐食物" + "-*-" + foodEntity.getName());
            }
            for (DiseaseEntity diseaseEntity:diseaseEntities2){
                diseaseSet.add(diseaseEntity.getId() + "::" + diseaseEntity.getName());
                relationShips.add(diseaseEntity.getId() + "-*-" + "不能吃食物" + "-*-" + foodEntity.getName());
            }
            result.put("disease",diseaseSet);
            result.put("food",foodSet);
            result.put("relationships",relationShips);
        }else if(identity == 5){//症状
            SymptomEntity symptomEntity = symptomRepository.getSymptomByName(name);
            if(symptomEntity == null) return null;
            List<DiseaseEntity> diseaseEntities = diseaseRepository.getSymptomDisease(name,num);
            Set<String> diseaseSet = new HashSet<>();
            Set<String> symptomSet = new HashSet<>();
            symptomSet.add(symptomEntity.getName());
            List<String> relationShips = new ArrayList<>();
            for (DiseaseEntity diseaseEntity:diseaseEntities){
                diseaseSet.add(diseaseEntity.getId() + "::" + diseaseEntity.getName());
                relationShips.add(diseaseEntity.getId() + "-*-" + "表现症状" + "-*-" + symptomEntity.getName());
            }
            result.put("disease",diseaseSet);
            result.put("symptom",symptomSet);
            result.put("relationships",relationShips);
        }
        return result;
    }
}
